<?php

class Ddup_Featured_Block_Products extends Mage_Catalog_Block_Product_Abstract 
{
	protected $_productGroupId;
	
	protected $_productGroup;
	
	public function getProductsCollection()
	{
		if (!$this->getProductGroup())
			return false;
		$productIds = $this->getProductGroup()->getProducts();
		if ( $productIds ) {
			$productIds = explode(',' , $productIds);
		}
		$productCollection = Mage::getResourceModel('catalog/product_collection')
            			->addAttributeToSelect('*')
						->addMinimalPrice()
            			->addFinalPrice()
            			->addUrlRewrite()
						->addIdFilter($productIds);
		if ($this->getProductGroup()->getIsRandom())
			$productCollection->getSelect()->order('rand()');
		$productCollection->setPageSize($this->getProductGroup()->getShowQty());
		return $productCollection;
	}
	
	public function getProductGroupTitle()
	{
		if (!$this->_productGroup)
			return false;
		return $this->_productGroup->getTitle();
	}
	
	public function getProductGroup()
	{
		$productGroup = Mage::getModel('featured/products')->load($this->_productGroupId);
		if ( $productGroup ) {
			return $this->_productGroup = $productGroup;
		}
		return false;
	}
	
	public function setProductGroupId($id){		
        if (intval($id) > 0) {
            $this->_productGroupId = intval($id);
        }
        return $this;
    }
}